package com.aistar.controller;

import com.aistar.pojo.Choice;
import com.aistar.pojo.Customer;
import com.aistar.pojo.QuestionDetail;
import com.aistar.pojo.QuestionSet;
import com.aistar.service.ChoiceCollectService;
import com.aistar.service.ChoiceService;
import com.aistar.service.QuestionDetailService;
import com.aistar.service.QuestionSetService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.apache.catalina.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/choose/chooseDetail")
public class ChoiceDetailController {

    @Autowired
    private ChoiceService choiceService;

    @Autowired
    private QuestionSetService questionSetService;

    @Autowired
    private QuestionDetailService questionDetailService;

    @Autowired
    private ChoiceCollectService choiceCollectService;

    @RequestMapping()
    public String chooseDetail() {
        return "choose/chooseDetail";
    }

    //获得choice的id
    @RequestMapping("{questionSetId}/{choiceId}")
    public ModelAndView getChoiceNum(@PathVariable("questionSetId") Integer questionSetId, @PathVariable("choiceId") Integer choiceId) {
        ModelAndView mav = new ModelAndView();

        ServerResponse serverResponse = questionDetailService.getByQuestionSetId(questionSetId);
        mav.addObject("serverResponse", serverResponse);

        ServerResponse serverResponseChoice = choiceService.getById(choiceId);
        Choice choice = (Choice) serverResponseChoice.getData();
        mav.addObject("choice", choice);
        mav.setViewName("choose/chooseDetail");
        return mav;
    }


    //题目的做题时间
    @PostMapping("/answer/{questionSetId}/{time}")
    @ResponseBody
    public ServerResponse getAnswer(@PathVariable("questionSetId") Integer questionSetId, @PathVariable("time") Double time,
                                    @RequestBody Map<Integer, String> chooseAnswer) {
        System.out.println(questionSetId);
        System.out.println(time);
        System.out.println(chooseAnswer);

        ServerResponse serverResponse;
        serverResponse = questionSetService.getById(questionSetId);
        QuestionSet questionSet = (QuestionSet) serverResponse.getData();

        questionSet.setQuestionSetThroughTime(time);

        Double mark = 0d;
        for (Map.Entry<Integer, String> entry : chooseAnswer.entrySet()) {
            Choice choice = (Choice) choiceService.getById(entry.getKey()).getData();
            if (choice.getChoiceCorrectAnswer().equals(entry.getValue()))
                mark += 10;

            QuestionDetail questionDetail = (QuestionDetail) questionDetailService.getByQuestionSetIdAndChoiceId(questionSetId, choice.getChoiceId()).getData();
            questionDetail.setOther1(entry.getValue());
            questionDetailService.modify(questionDetail);
        }
        questionSet.setQuestionSetMark(mark);

        serverResponse = questionSetService.modify(questionSet);

        return serverResponse;
    }


    @RequestMapping("/answer/{questionSetId}")
    public ModelAndView getMyAnswer(@PathVariable("questionSetId") Integer questionSetId) {
        ModelAndView mav = new ModelAndView();
        QuestionSet questionSet = (QuestionSet) questionSetService.getById(questionSetId).getData();
        ServerResponse serverResponse = questionDetailService.getByQuestionSetId(questionSetId);
        if (MessageUtil.STATUS_SUCCESS.equals(serverResponse.getStatus())) {
            QuestionDetail questionDetail = (QuestionDetail) ((List) serverResponse.getData()).get(0);
            mav.addObject("choiceId", questionDetail.getChoiceId());
        }
        mav.addObject("questionSet", questionSet);
        Double questionSetThroughTime = questionSet.getQuestionSetThroughTime();
        mav.addObject("questionSetThroughTime", questionSetThroughTime);
        Double questionSetMark = questionSet.getQuestionSetMark();
        mav.addObject("questionSetMark", questionSetMark);
        mav.setViewName("choose/chooseResult");
        return mav;
    }
}
